namespace Example
{
    using System;
    using System.Net;
    using System.Net.Mime;
    using SimpleWebServices.Net;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class SiteFacts
    {
        private static readonly Resource _resource = new GenericResource
        {
            Description = "This is the site information page.",
            Location = "/site",
            Title = "Site Information",
            Links =
            {
                new Link { Relationship = "canonical", Hyperlink = new Uri("http://example.simplewebservices.org/site.en.html") }
            }
        };

        [Fact]
        public void Get_LanguageNegotiation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsLanguageNegotiationToEnglish()
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_ContentNegotiation_html()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsContentNegotiationToTextHtml()
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_ContentNegotiation_json()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en")
                .Accept("application/json")
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsSeeOther("http://example.simplewebservices.org/site.en.json")
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_ContentNegotiation_txt()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en")
                .Accept("text/plain")
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsSeeOther("http://example.simplewebservices.org/site.en.txt")
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_ContentNegotiation_xhtml()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en")
                .Accept("application/xhtml+xml")
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsSeeOther("http://example.simplewebservices.org/site.en.xhtml")
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_ContentNegotiation_xml()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en")
                .Accept("application/xml")
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .IsSeeOther("http://example.simplewebservices.org/site.en.xml")
                .HasNoCookies()
                .Result);
        }

        [Fact]
        public void Get_HtmlRepresentation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en.html")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .Is(HttpStatusCode.OK)
                .HasCacheControlNone()
                .HasContentLanguageOfEnglish()
                .HasContentMD5()
                .ResponseIsTextHtml(_resource)
                .HasRobotsTag("NOFOLLOW,NOINDEX")
                .Evaluate<double>(1, "count(/html/body/dl[@id='server']/dd[@id='host'])")
                .Evaluate<double>(1, "count(/html/body/dl[@id='server']/dd[@id='machine'])")
                .Evaluate<bool>(true, "0 != count(/html/body/ul[@id='assemblies']/li)")
                .HasNoCookies()
                .HasContentLocation("http://example.simplewebservices.org/site.en.html")
                .Result);
        }

        [Fact]
        public void Get_JsonRepresentation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en.json")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .Is(HttpStatusCode.OK)
                .HasCacheControlNone()
                .HasContentLanguageOfEnglish()
                .HasContentMD5()
                .ResponseIsApplicationJson(new ContentType("application/vnd.simplewebservices-site+json"))
                .HasNoCookies()
                .HasContentLocation("http://example.simplewebservices.org/site.en.json")
                .Result);
        }

        [Fact]
        public void Get_TextRepresentation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en.txt")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .Is(HttpStatusCode.OK)
                .HasCacheControlNone()
                .HasContentLanguageOfEnglish()
                .HasContentMD5()
                .ResponseIsTextPlain()
                .HasNoCookies()
                .HasContentLocation("http://example.simplewebservices.org/site.en.txt")
                .Result);
        }

        [Fact]
        public void Get_XhtmlRepresentation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en.xhtml")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .Is(HttpStatusCode.OK)
                .HasCacheControlNone()
                .HasContentLanguageOfEnglish()
                .HasContentMD5()
                .ResponseIsApplicationXhtml(_resource)
                .HasRobotsTag("NOFOLLOW,NOINDEX")
                .Evaluate<double>(1, "count(/html/body/dl[@id='server']/dd[@id='host'])")
                .Evaluate<double>(1, "count(/html/body/dl[@id='server']/dd[@id='machine'])")
                .Evaluate<bool>(true, "0 != count(/html/body/ul[@id='assemblies']/li)")
                .HasNoCookies()
                .HasContentLocation("http://example.simplewebservices.org/site.en.xhtml")
                .Result);
        }

        [Fact]
        public void Get_XmlRepresentation()
        {
            Assert.True(HttpExpectations.New("http://example.simplewebservices.org/site.en.xml")
                .AcceptAnyContent()
                .AcceptAnyLanguage()
                .Cookieless()
                .Get()
                .Is(HttpStatusCode.OK)
                .HasCacheControlNone()
                .HasContentLanguageOfEnglish()
                .HasContentMD5()
                .ResponseIsApplicationXml(new ContentType("application/vnd.simplewebservices-site+xml"))
                .Evaluate<bool>(true, "0 != count(/site[@host][@machine]/assemblies/assembly)")
                .HasNoCookies()
                .HasContentLocation("http://example.simplewebservices.org/site.en.xml")
                .Result);
        }
    }
}